package com.zjsru.oneDay202204;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: likew
 * @Date: 2022/4/20
 *
 * 输入：input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
 * 输出：32
 * 解释：存在两个文件：
 * "dir/subdir1/file1.ext" ，路径长度 21
 * "dir/subdir2/subsubdir2/file2.ext" ，路径长度 32
 * 返回 32 ，因为这是最长的路径
 *
 * 输入：input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
 * 输出：20
 * 解释：只有一个文件，绝对路径为 "dir/subdir2/file.ext" ，路径长度 20
 *
 *
 */
public class lengthLongestPath {
    public int lengthLongestPath(String input) {
        int len = input.length();
        Map<Integer,String> map = new HashMap<>();
        String ans = null;
        for (int i = 0; i < len;) {
            int level = 0;
            while(i < len && input.charAt(i) == '\t' && ++level >= 0){
                i ++;
            }
            int j  = i;
            boolean isDir = true;
            while(j < len && input.charAt(j) != '\n'){
                if(input.charAt(j++) == '.'){
                    isDir = false;
                }
            }
            String cur = input.substring(i,j);
            String prev = map.getOrDefault(level - 1,null);
            String path = prev == null ? cur : prev + "/" + cur;
            if(isDir) map.put(level,path);
            else if(ans == null || path.length() > ans.length()) ans = path;
            i = j+1;
        }
        return ans == null ? 0 : ans.length();
    }
    
    public static void main(String[] args) {
        lengthLongestPath lengthLongestPath = new lengthLongestPath();
        String input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext";
        System.out.println(lengthLongestPath.lengthLongestPath(input));
    }
}
